package is202.hrms.web;

import is202.hrms.ejb.ModulenEJB;
import is202.hrms.entity.Modulen;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * The backing bean for modulenartment.xhtml. The Named annotation marks it
 * as a CDI backing bean. The ConversationScoped annotation controls
 * the lifecycle of the bean. The lifetime of a ConversationScoped bean is
 * explicitly managed in the code.
 *
 * @author even
 */
@Named("modulenbean")
@SessionScoped
public class ModulenBean implements Serializable {

    // The @Inject annotation is a request to the server to
    // give us a Conversation object, and store it in conv
    @Inject private Conversation conv;
    @EJB ModulenEJB modulenEjb;
    private Modulen modulen;
    private boolean updating;

    // Modulen properties - These properties will be copied to a Modulen
    // object, when the time comes to save the data in the database.
    private int modulenId;
    private String task;
    private String requirements;
    private List<Modulen> modulens;
    private Date dato;

    public ModulenBean() {
    }

    /**
     * called when the modulen selects a modulenartment from the list in
     * modulenartments.xhtml.
     */
    public void setModulenId(int modulenId) {
        if (conv.isTransient()) {
            conv.begin();
        }

        modulen = modulenEjb.find(modulenId);
        if (null == modulen) {
            // we will get here if modulenId is not a valid, or if
            // modulenId is valid but there is no modulenartment with that modulenno
            updating = false;
            modulenId = 0;
           // name = "";
         //   employees = new ArrayList<Employee>();
        }
        else {
            updating = true;
            this.modulenId = modulen.getModulenId();
            task = modulen.getTask();
            requirements = modulen.getRequirements();
            dato = modulen.getDato();
          //  employees = modulen.getEmployees();
        }
    }

    public Date getDato() {
        return dato;
    }

    public void setDato(Date dato) {
        this.dato = dato;
    }
    
    
    
    public int getModulenId() {
        return modulenId;
    }

    public ModulenEJB getModulenEjb() {
        return modulenEjb;
    }

    public void setModulenEjb(ModulenEJB modulenEjb) {
        this.modulenEjb = modulenEjb;
    }

    public Modulen getModulen() {
        return modulen;
    }

    public void setModulen(Modulen modulen) {
        this.modulen = modulen;
    }

    public boolean isUpdating() {
        return updating;
    }

    public void setUpdating(boolean updating) {
        this.updating = updating;
    }

    public String getTask() {
        return task;
    }

    public void setTask(String task) {
        this.task = task;
    }

    public String getRequirements() {
        return requirements;
    }

    public void setRequirements(String requirements) {
        this.requirements = requirements;
    }

    public List<Modulen> getModulens() {
        return modulens;
    }

    public void setModulens(List<Modulen> modulens) {
        this.modulens = modulens;
    }
    
    

    private Modulen createModulenObject() {
        // Create a modulenartment object containing the data
        // from the form. The object can be passed on to
        // modulenListBean
        modulen = new Modulen();
        modulen.setModulenId(modulenId);
        modulen.setTask(task);
        modulen.setRequirements(requirements);
        modulen.setDato(dato);
        return modulen;
    }

    public View saveModulen() {
         modulen = createModulenObject();
        if (modulen.getModulenId() > 0) modulenEjb.update(modulen);
        else modulenEjb.insert(modulen);
        conv.end();
        return View.modulchange;
    }

    public View deleteModulen() {
        modulen = createModulenObject();
        modulenEjb.delete(modulen);
        conv.end();

        return View.modulchange;
    }
    
            
    
}

